# from datetime import datetime, timedelta
# from django.core.cache import cache
# from rest_framework.authentication import BaseAuthentication
# from rest_framework.exceptions import APIException
#
# from myapp3.models import UserToken
#
#
# class UserAuth(BaseAuthentication):
#
#     def authenticate(self,request):
#         token = request.META.get('HTTP_AUTHENTICATION')  #将token放入请求头中
#         print(token)
#         user = cache.get(token)
#         if user:
#             print('这是缓存')
#             return user,token
#         token_obj = UserToken.objects.filter(token=token).first()
#         if not token_obj:
#             raise APIException('认证失败')
#         created_time = token_obj.created_at
#         print('创建时间',created_time)
#         now_time = datetime.now()
#         print('现在的时间',now_time)
#         delta = now_time - created_time
#         print('过去多少天时间',delta)
#         if delta < timedelta(seconds=5):
#             remain_time = timedelta(weeks=2) - delta
#             print('剩余时间',remain_time)
#             cache.set(token_obj.token,token_obj.user,min(remain_time.total_seconds(),3600*24*7))
#             print('这是数据库')
#             return token_obj.user,token
#         else:
#             raise APIException('认证超时')
